package org.sam.swing;

import java.awt.BorderLayout;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.sam.swing.table.JSTable;
import org.sam.swing.table.JSTableBuilder;
import org.sam.swing.table.JSTableColumn;
import org.sam.swing.table.JSTableColumnModel;
import org.sam.swing.table.JSTableModel;
import org.sam.swing.table.JSTableModelEvent;
import org.sam.swing.table.JSTableModelLinster;
import org.sam.swing.table.matrixImpl.JSTableMatrixBuilderImpl;
import org.sam.swing.table.matrixImpl.JSTableMatrixHColumn;
import org.sam.swing.table.matrixImpl.JSTableMatrixModel;
import org.sam.swing.table.matrixImpl.JSTableMatrixVColumn;
import org.sam.swing.table.matrixImpl.JSTableMatrixValueColumn;
import org.sam.swing.table.matrixImpl.JSTableModelMatrixAdapter;
import org.sam.swing.table.tools.JSTableToolBar;

/**
 * 交叉表格demo
 * 
 * @author sam
 *
 */
public class JPanelMatrix extends JPanel {

	private static final long serialVersionUID = 1446855388859406393L;

	/**
	 * 交叉表格demo
	 */
	public JPanelMatrix() {
		super();
		this.initCompents();
	}

	/**
	 * 表格model
	 */
	private JSTableModel<?> tableModel;

	/**
	 * 列model
	 */
	private JSTableColumnModel colModel;

	/**
	 * 表格
	 */
	private JSTable table;

	/**
	 * 初始化控件显示
	 */
	private void initCompents() {
		this.setLayout(new BorderLayout());

		JSTableMatrixVColumn vColumn = new JSTableMatrixVColumn();
		vColumn.setHeaderValue("");
		vColumn.setIdentifier("gender");
		vColumn.setTitle("");
		vColumn.setWidth(75);
		vColumn.setMinWidth(25);
		vColumn.setDefaultValue("");

		JSTableMatrixHColumn hColumn = new JSTableMatrixHColumn();
		hColumn.setHeaderValue("");
		hColumn.setIdentifier("code");
		hColumn.setTitle("");
		hColumn.setWidth(75);
		hColumn.setMinWidth(25);
		hColumn.setDefaultValue("");

		JSTableMatrixValueColumn valueColumn = new JSTableMatrixValueColumn();
		valueColumn.setHeaderValue("");
		valueColumn.setIdentifier("age");
		valueColumn.setTitle("");
		valueColumn.setWidth(75);
		valueColumn.setMinWidth(25);
		valueColumn.setDefaultValue(0);

		try {
			// 生成表格对象
			JSTableBuilder<Map<String, List<TestEntity>>> builder = new JSTableMatrixBuilderImpl<String, TestEntity>(
					vColumn, hColumn, valueColumn) {

				@Override
				public JSTableModelLinster<Map<String, List<TestEntity>>> buildModelLinster(
						JSTableModel<Map<String, List<TestEntity>>> tableModel) throws Exception {

					JSTableModelLinster<Map<String, List<TestEntity>>> adapter = new JSTableModelMatrixAdapter<String, TestEntity>() {

						/**
						 * 生成数据
						 */
						@Override
						public Map<String, List<TestEntity>> onRetrieve() throws Exception {
							List<TestEntity> result = new LinkedList<>();
							for (int i = 1; i <= 15; i++) {
								TestEntity entity = new TestEntity();
								entity.setCode(i % 3 == 0 ? "测试1" : (i % 2 == 1 ? "测试2" : "测试3"));
								entity.setGender(i % 3 == 0 ? "男" : (i % 2 == 1 ? "女" : "无"));
								entity.setAge(i);
								result.add(entity);
							}

							Map<String, List<TestEntity>> collect = result.stream()
									.collect(Collectors.groupingBy(TestEntity::getGender));
							return collect;
						}

						/**
						 * 更新colmodel
						 */
						/*
						 * (non-Javadoc)
						 * 
						 * @see org.sam.swing.table.matrixImpl.
						 * JSTableModelMatrixAdapter#afterRetrieve(org.sam.swing
						 * .table.JSTableModelEvent)
						 */
						@SuppressWarnings("unchecked")
						@Override
						public void afterRetrieve(JSTableModelEvent event) throws Exception {
							super.afterRetrieve(event);
							colModel.removeColumn(vColumn);
							colModel.removeColumn(hColumn);
							colModel.removeColumn(valueColumn);

							JSTableMatrixModel<String, List<TestEntity>> model = (JSTableMatrixModel<String, List<TestEntity>>) event
									.getTableModel();
							JSTableColumn[] matrixColumns = model.getMatrixColumns();
							for (int i = 0; i < matrixColumns.length; i++) {
								colModel.addColumn(matrixColumns[i]);
							}

							table.setColumnModel(colModel);
						}

						@SuppressWarnings("unchecked")
						@Override
						public void beforeUpdate(JSTableModelEvent event) throws Exception {
							Map<String, List<TestEntity>> modified = (Map<String, List<TestEntity>>) event
									.getTableModel().getModified();
							for (Entry<String, List<TestEntity>> entry : modified.entrySet()) {
								for (TestEntity e : entry.getValue()) {
									System.out.println(
											"[code=" + e.getCode() + ";gender=" + e.getGender() + ";age=" + e.getAge() + "]");
								}
							}
						}

					};
					tableModel.setTableModelLinster(adapter);
					return adapter;
				}
			};

			tableModel = builder.getTableModel();
			colModel = new JSTableColumnModel();
			table = builder.buildTable();
			this.add(new JScrollPane(table), BorderLayout.CENTER);

			// 加入工具条
			JSTableToolBar toolBar = new JSTableToolBar(table);
			toolBar.enableRetrieve();
			toolBar.enableSelect(7);
			toolBar.enableEdit();
			toolBar.enableAdd();
			toolBar.enableDel();
			toolBar.enableSave();
			toolBar.enableSearch();
			this.add(toolBar, BorderLayout.NORTH);

			tableModel.retrieve();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
